import type { LocationQueryValue } from "vue-router";
import http from "..";
import http1 from "../index1";
import type { AddEquipmentParam, CustomSuccessData, DeleteEquipmentParam, Equipment, EquipmentDetailInfo, EquipmentHistory, ListParam, ListResponse, QueryEquipmentHistory,QueryEquipmentHistory1 } from "../types";

const baseURL="/equipment";
let token = uni.getStorageSync('token');
export const addEquipment= async(addEquipment:AddEquipmentParam):Promise<CustomSuccessData<string>>=>{
    const res = await http.reqPost<string>(
        baseURL+'/add',
        {
            equipmentName:addEquipment.equipmentName,
            equipmentDes:addEquipment.equipmentDes
        },
    )
    return res
}
// :Promise<CustomSuccessData<ListResponse<Equipment>>>
export const listEquipment = async(listParam:ListParam)=>{
    // const res = await http1.reqPost(
    //     import.meta.env.VITE_BASE_URL + '/equipment' +'/list',
    //     {
    //         name:listParam.name,
    //         index:listParam.index,
    //         pageSize:listParam.pageSize
    //     }
    // )
    // return res
    const res  = await uni.request({
        url: import.meta.env.VITE_BASE_URL + '/equipment' +'/list',
        method: 'POST',
        header: {
            // 'Content-Type': 'application/json'
            Token: token
        },
        data: {
            name:listParam.name,
            index:listParam.index,
            pageSize:listParam.pageSize
        }
    });
    return res.data;
}


export const deleteEquipment = async(deleteEquipmentParam:DeleteEquipmentParam):Promise<CustomSuccessData<string>>=>{
    const res = await http.reqPost<string>(
        baseURL+'/delete',
        {
            equipmentID:deleteEquipmentParam.equipmentUUID
        }
    )
    return res
}
// :Promise<CustomSuccessData<EquipmentDetailInfo>>
export const getEquipmentDetail = async(equipmentUUID:LocationQueryValue | LocationQueryValue[])=>{
    // Use uni.request to make a network request
    // return new Promise((resolve, reject) => {
    //     uni.request({
    //         url: import.meta.env.VITE_BASE_URL + '/equipment' +'/getDetail', // Developer server interface address
    //         method: 'POST', // Request method
    //         data: {
    //             equipmentUUID:equipmentUUID // Request parameter
    //         },
    //         success: (res) => {
    //             // Received the callback function successfully returned by the developer server
    //             resolve(res)
    //         },
    //         fail: (err) => {
    //             // Interface call failure callback function
    //             reject(err)
    //         }
    //     })
    // })
    const res  = await uni.request({
        url: import.meta.env.VITE_BASE_URL + '/equipment' +'/getDetail',
        method: 'POST',
        header: {
            // 'Content-Type': 'application/json'
            Token: token
        },
        data: {
            equipmentUUID:equipmentUUID
        }
    });
    return res.data; // uni.request返回一个数组，第一个元素是错误信息，第二个元素是响应数据
}

// :Promise<CustomSuccessData<EquipmentHistory>>
//这个函数用于日期选择页发送请求
export const getEquipmentHistory = async(queryParam:QueryEquipmentHistory)=>{
    // Use uni.request to make a network request
    // return new Promise((resolve, reject) => {
    //     uni.request({
    //         url: import.meta.env.VITE_BASE_URL + '/equipment' +'/history', // Developer server interface address
    //         method: 'POST', // Request method
    //         data: {
    //             equipmentUUID:queryParam.equipmentUUID,
    //             Date:queryParam.Date // Request parameter
    //         },
    //         success: (res) => {
    //             // Received the callback function successfully returned by the developer server
    //             resolve(res)
    //         },
    //         fail: (err) => {
    //             // Interface call failure callback function
    //             reject(err)
    //         }
    //     })
    // })
    // const res = await http.reqPost<EquipmentHistory>(
    //     baseURL+'/history',
    //     {
    //         equipmentUUID:queryParam.equipmentUUID,
    //         Date:queryParam.Date
    //     }
    // )
    // return res
    const res  = await uni.request({
        url: import.meta.env.VITE_BASE_URL + '/equipment' +'/history',
        method: 'POST',
        header: {
            // 'Content-Type': 'application/json'
            Token: token
        },
        data: {
            equipmentUUID:queryParam.equipmentUUID,
            startTime:queryParam.startDate,
            endTime:queryParam.endDate
        }
    });
    return res.data; // uni.request返回一个数组，第一个元素是错误信息，第二个元素是响应数据
}

//这个函数用作点击进去的报告页
export const getEquipmentHistory1 = async(queryParam:QueryEquipmentHistory1)=>{
const res  = await uni.request({
        url: import.meta.env.VITE_BASE_URL + '/equipment' +'/history1',
        method: 'POST',
        header: {
            // 'Content-Type': 'application/json'
            Token: token
        },
        data: {
            equipmentUUID:queryParam.equipmentUUID,
            Date:queryParam.Date1
        }
    });
    return res.data; // uni.request返回一个数组，第一个元素是错误信息，第二个元素是响应数据
}